package net.wanho.controller;

import lombok.SneakyThrows;
import net.wanho.entity.Student;
import net.wanho.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-06-27 14:15
 * Description：<描述>
 */
@RestController
@RequestMapping("/param")
public class ParamController {

    /**
     * JavaEE组件
     */
    @SneakyThrows
    @GetMapping("/test1")
    public void test1(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name");
        String method = req.getMethod();
        String userAgent = req.getHeader("User-Agent");
        System.out.println(name);
        System.out.println(method);
        System.out.println(userAgent);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("<h1>成功</h1>");
    }

    /**
     * String、基本类型及包装类
     */
    @RequestMapping("/test2")
    public String test2(@RequestParam("name") String name, @RequestParam(required = false, defaultValue = "18") int age, Double height){
        System.out.println(name);
        System.out.println(age);
        System.out.println(height);
        return "success";
    }

    @RequestMapping("/test3/{id}/{name}")
    public String test3(@PathVariable("id") int id, @PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return "success";
    }

    @RequestMapping("/test4")
    public String test4(@RequestHeader("User-Agent") String userAgent){
        System.out.println(userAgent);
        return "success";
    }

    @RequestMapping("/test5")
    public String test5(@CookieValue String sex){
        System.out.println(sex);
        return "success";
    }

    @DeleteMapping("/test6")
    // public String test6(@RequestBody String str){
    // public String test6(@RequestBody Student stu){
    public String test6(@RequestBody List<Integer> ids){
        // System.out.println(stu);
        System.out.println(ids);
        return "success";
    }

    @RequestMapping("/test7")
    public String test7(User user){
        System.out.println(user);
        return "success";
    }
}
